#include<bits/stdc++.h>
#define int long long
#define y1 _______
#define y2 ________
using namespace std;
const int N = 1e3+5,mod = 147744151;
int n,m,p,q,a[N][N],s[N][N],ans;
int qpow(int x,int y)
{
	int res = 1;
	while(y)
	{
		if(y&1) res = res*x%p;
		y>>=1;
		x = x*x%p;
	}
	return res;
}
int qmod(int x,int y)
{
	return x*qpow(y,p-2)%p;
}
signed main()
{
	freopen("tsuki.in","r",stdin);
	freopen("tsuki.out","w",stdout);
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n>>m>>p;
	for(int i = 1;i<=n;i++)
		for(int j = 1;j<=m;j++)
			cin>>a[i][j];
	cin>>q;
	if(n<=200&&q<=200)
	{
		for(int _ = 1;_<=q;_++)
		{
			int x1,y1,x2,y2,sum = 1;
			cin>>x1>>y1>>x2>>y2;
			for(int i = x1;i<=x2;i++)
				for(int j = y1;j<=y2;j++)
					(sum*=a[i][j])%=p;
			(ans+=sum^_)%=mod;
		}
		return cout<<ans,0;
	}
	if(p==998244353)
	{
		for(int i = 0;i<=n;i++)
			s[i][0] = 1;
		for(int i = 0;i<=m;i++)
			s[0][i] = 1;
		for(int i = 1;i<=n;i++)
			for(int j = 1;j<=m;j++)
				s[i][j] = qmod(s[i][j-1]*s[i-1][j]%p,s[i-1][j-1])*a[i][j]%p;
		for(int _ = 1;_<=q;_++)
		{
			int x1,y1,x2,y2,sum;
			cin>>x1>>y1>>x2>>y2;
			sum = qmod(s[x2][y2]*s[x1-1][y1-1]%p,s[x2][y1-1]*s[x1-1][y2]%p);
			(ans+=sum^_)%=mod;
		}
		return cout<<ans,0;
	}
	return 0;
}

